package org.w515.lab0_backend.controller;

import java.util.List;
import org.springframework.web.bind.annotation.*;
import org.w515.lab0_backend.common.Result;
import org.w515.lab0_backend.dto.ProductCreateDTO;
import org.w515.lab0_backend.dto.ProductPageQueryDTO;
import org.w515.lab0_backend.service.ProductService;
import org.w515.lab0_backend.vo.ProductItemVO;
import org.w515.lab0_backend.vo.ProductPageVO;

@RestController
@RequestMapping("/api/products")
public class ProductController {

    private final ProductService productService;

    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    @GetMapping
    public Result<ProductPageVO> listProducts(ProductPageQueryDTO queryDTO) {
        return Result.success(productService.listProducts(queryDTO));
    }

    @GetMapping("/search")
    public Result<List<ProductItemVO>> searchProducts(@RequestParam String name) {
        return Result.success(productService.searchProducts(name));
    }

    @PostMapping
    public Result<Long> createProduct(@RequestBody ProductCreateDTO dto) {
        return Result.success(productService.createProduct(dto));
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> deleteProduct(@PathVariable Long id) {
        return Result.success(productService.deleteProduct(id));
    }
}